<?php 
require_once('Class/session.Class.php');
require_once('Class/connexionDB.Class.php');

/**
	class used to create & save form rating in DB
*/
class evaluation
{
	var $errorTab 			= array();
	var $criteria 			= array();
	var $name 				= '';
	var $dateCreated 		= '';
	var $author 			= '';
	var $id_evaluation_form = 0;
	var $criteriaMark		= array();
	const PARAM_END			= 'end';
	const PARAM_MARK		= 3;

	/**
		Count item in array 
	*/
	static function countItem($array, $item)
	{
		$counted = array_count_values($array);
		if (isset($counted[$item]))
		{
			return $counted[$item];
		}
		else
		{
			return 0;
		}
		
	}
	/**
		Return a list of evaluation form
	*/
	static function listedEvalForm()
	{
		$db = new connexionDB();

		$result = $db->getEvaluationForm();
		return $result;
	}

	/**
	*/
	static function listedTopic()
	{
		$db = new connexionDB();

		$result = $db->listedTopic();
		return $result;
	}

	static function getLevelMarkByCriteria($array)
	{
		$result = array();
		$i 		= 0;
		$max 	= 3;
		$idx 	= 0;
		$nb 	= 0;

		foreach ($array as $key => $value)
		{
			if ((strpos($key, 'nameCriteria') !== false || strpos($key, 'markCriteria') !== false || strpos($key, 'descriptionMarkCriteria') !== false) && $value != '')
			{
				if ($nb == $max)
				{
					$idx++;
					$nb = 0;
				}
				$result[$i][$idx][$key] = $value;
				$nb++;
			}
			if ($value == 'end')
			{
				$i++;
			}
		}
		return $result;
	}

	/**
		return an array[] of Criteria with : 
					-name
					-description
					-coeff
	*/
	static function getCriteriaFromParam($data)
	{
		$result 		= array();
		$maxCriteria 	= evaluation::countItem($data, evaluation::PARAM_END);
		$idx 			= 0;

		foreach ($data as $key => $value)
		{
			if (strpos($key, 'titleCriteria') !== false)
			{
				$result[$idx]['name'] = $value;
			}
			if (strpos($key, 'descriptionCriteria') !== false)
			{
				$result[$idx]['description'] = $value;
			}
			if (strpos($key, 'coeffCriteria') !== false)
			{
				$result[$idx]['coeff'] = floatval($value);
			}
			if ($value == evaluation::PARAM_END)
			{
				$idx++;
			}
		}
		return $result;
	}

	function getEvaluationData($id)
	{
		$db = new connexionDB();

		$result = $db->getEvaluationData($id);
		return $result;
	}

	/**
		return an array[][] of criteria mark with :
			- id_criteria 
			- id_evaluation_form
			- name 
			- mark
			- description
	*/
	function getCriteriaMarkFromParam($data)
	{
		$result			= array();
		$criteriaName 	= '';
		$idx 			= 0;
		$nbLimit		= 0;
		
		foreach ($data as $key => $value)
		{
			if (strpos($key, 'titleCriteria') !== false)
			{
				$criteriaName = $value;
			}
			if (strpos($key, 'nameCriteria') !== false)
			{
				$result[$idx]['name'] = $value;
				$nbLimit++;
			}
			if (strpos($key, 'markCriteria') !== false)
			{
				$result[$idx]['mark'] = $value;
				$nbLimit++;
			}
			if (strpos($key, 'descriptionMarkCriteria') !== false)
			{
				$result[$idx]['description'] = $value;
				$nbLimit++;
			}
			if ($nbLimit == evaluation::PARAM_MARK)
			{
				$result[$idx]['id_criteria'] = $this->db->getIdCriteriaByName($criteriaName, $this->id_evaluation_form);
				$result[$idx]['id_evaluation_form'] = $this->id_evaluation_form;
				$idx++;
				$nbLimit = 0;
			}
		}
		
		return $result;
	}

	/**
		Add a new evaluation to the data-base
		3 steps to do it : 
			- add the info in evaluation_form table
			- add the info in criteria table
			- add the info in criteria_work table
	*/
	function addNewEvaluationType($data)
	{
		$session 	= session::getInstance();
		$this->db 	= new connexionDB();

		if (isset($data['evaluationName']) && $data['evaluationName'] != '')
		{
			$this->name 		= $data['evaluationName'];
			$this->dateCreated 	= date('Y-m-d');
			$this->author 		= strtolower($session->firstname) . ' ' . strtolower($session->lastname);
			
			$this->db->addNewEvaluationForm($this->name, $this->dateCreated, $this->author);
			
			$this->id_evaluation_form 	= $this->db->getIdEvaluationForm($this->name, $this->dateCreated, $this->author);
			
			$this->criteria 			= $this->getCriteriaFromParam($data);

			$this->db->addCriteria($this->criteria, $this->id_evaluation_form);
			
			$this->criteriaMark = $this->getCriteriaMarkFromParam($data);

			$this->db->addCriteriaMark($this->criteriaMark);
		}
	}

	/**
		Return an array of errors if not return NULL
	*/
	function isValidForm($data)
	{
		if (!isset($data['evaluationName']) || $data['evaluationName'] == '')
		{
			$this->errorTab['evaluationName'] = 'error';
		}

		if (empty($this->errorTab))
		{
			return NULL;
		}	
		else
		{
			return $this->errorTab;
		}
	}

	/**
		Return array without the empty row
	*/
	static function cleanArray($array)
	{
		$result = array();
		$critToDelete = array();

		foreach ($array as $key => $value)
		{
			if ($value != '')
			{
				$result[$key] = $value;
			}
			else
			{
				if (strpos($key, 'titleCriteria') !== false)
				{
					$critToDelete[] = str_replace('titleCriteria', '', $key);
				}
			}
		}
		foreach ($critToDelete as $key => $value)
		{
			unset($result['endCriteria' . $value]);
		}
		return $result;
	}

	function getCriteriaForEval($id)
	{
		$db 	= new connexionDB();

		$criteria = $db->getCriteriaForEval($id);
		if ($criteria == null)
		{
			return array();
		}
		return $criteria;
	}

	function getLevelMark($id)
	{
		$db = new connexionDB();

		$lvlMark = $db->getLevelMark($id);
		if ($lvlMark == null)
		{
			return array();
		}
		return $lvlMark;
	}

	static function getHeader($array)
	{
		$result = array();

		if ($array == null)
		{
			return $result;
		}
		foreach ($array as $key => $value)
		{
			if (isset($value['name']))
			{
				$result[] = $value['name'];
			}
		}
		return $result;
	}

	/**
		return null if it's not a mark or an array containing detailed data associate to the mark
	*/
	static function isAMark($criteria, $mark, $id_criteria)
	{
		$db = new connexionDB();

		$result = $db->getCriteriaMarkData($criteria, $mark, $id_criteria);
		return $result;
	}

	function deleteData($del_header, $del_criteria, $id_eval)
	{
		$db = new connexionDB();

		foreach ($del_criteria as $key => $value)
		{
			if ($value != '')
			{
				$db->deleteCriteria(intval($value));
			}
		}

		foreach ($del_header as $key => $value)
		{
			if ($value != '')
			{
				$db->deleteLevelMark($value, $id_eval);
			}
		}
	}

	function addEvaluation($criteria_array, $level_mark, $set_to_zero, $id_eval, $del_header, $del_criteria)
	{
		$db 						= new connexionDB();

		$evaluation_id				= 0;

		$criteria_id 				= 0;
		$criteria_name 				= '';
		$criteria_description 		= '';
		$criteria_coeff 			= '';
		$criteria_type 				= '';
		$criteria_mark_description 	= array();

		$mark_id 					= 0;
		$mark_description 			= '';
		$criteria_zero 				= 0;
		$mark_level 				= array();

		$this->deleteData($del_header, $del_criteria, $id_eval);

		if (is_numeric($id_eval)) //Update
		{
			$evaluation_id = $id_eval;


			foreach ($level_mark as $keya => $valuea)
			{
				foreach ($valuea as $i => $valuec)
				{
					switch ($i)
					{
						case 0:
							$id_criteria_mark = intval($valuec);
							break;
						case 1:
							$name = $valuec;
							break;
						case 2:
							$mark = $valuec;
							break;
					}
				}
				$mark_level[] 	= array(	'id' 	=> $id_criteria_mark, 
											'name'	=> $name,
											'mark' 	=> $mark);
			}

			//Add or Up criteria
			foreach ($criteria_array as $criteria)
			{
				foreach ($criteria as $key => $value)
				{
					switch ($key)
					{
						case 0:
							$criteria_id 				= intval($value);
							break;
						case 1:
							$criteria_name 				= $value;
							break;
						case 2:
							$criteria_description 		= $value;
							break;
						case 3:
							$criteria_coeff 			= floatval($value);
							break;
						case 4:
							$criteria_type 				= $value;
							break;
						case 5:
							$criteria_mark_description 	= $value;
							break;
						case 6:
							$criteria_zero 				= $value;
							break;
					}
				}
				
				if ($criteria_type == 'old')
				{
					$db->updateCriteria($criteria_id, $criteria_name, $criteria_description, $criteria_coeff);
				}
				else
				{
					$criteria_id = $db->insertCriteria($criteria_name, $criteria_description, $criteria_coeff, $evaluation_id);
				}
				
				if ($criteria_zero == 1)
				{
					$db->setToZero($criteria_id);
				}

				$i  = 0;

				foreach ($criteria_mark_description as $keya => $valuea) 
				{
					foreach ($valuea as $keyb => $valueb)
					{
						switch ($keyb) 
						{
							case 0:
								$id_level_mark = $valueb;
								break;
							case 1:
								$description_mark = $valueb;
								break;
							case 2:
								$checked = $valueb;
								break;
						}
					}
					
					if ($checked == 'checked')
					{
						$checked = 1;
					}
					else
					{
						$checked = 0;
					}

					if (isset($mark_level[$i]['name']))
					{
						if ($id_level_mark == 'new')
						{
							$db->insertCriteriaMark($criteria_id, $evaluation_id, $mark_level[$i]['name'], $mark_level[$i]['mark'], $description_mark, $checked);
						}
						else
						{
							if (isset($mark_level[$i]['id']))
							{
								if ($mark_level[$i]['id'] != 'new')
								{
									$db->updateCriteriaMark($id_level_mark, $description_mark, $checked);	
								}
								else
								{
									$db->insertCriteriaMark($criteria_id, $evaluation_id, $mark_level[$i]['name'], $mark_level[$i]['mark'], $description_mark, $checked);		
								}
							}
						}
					}
					$i++;
				}
			}
		}
		else //Save as
		{
			$evaluation_id = $db->addNewEvaluationForm($id_eval, date('Y-m-d'), 'Admin');

			foreach ($level_mark as $keya => $valuea)
			{
				foreach ($valuea as $i => $valuec)
				{
					switch ($i)
					{
						case 0:
							$id_criteria_mark = intval($valuec);
							break;
						case 1:
							$name = $valuec;
							break;
						case 2:
							$mark = $valuec;
							break;
					}
				}
				$mark_level[] 	= array(	'id' 	=> $id_criteria_mark, 
											'name'	=> $name,
											'mark' 	=> $mark);
			}

			//Add or Up criteria
			foreach ($criteria_array as $criteria)
			{
				foreach ($criteria as $key => $value)
				{
					switch ($key)
					{
						case 0:
							$criteria_id 				= intval($value);
							break;
						case 1:
							$criteria_name 				= $value;
							break;
						case 2:
							$criteria_description 		= $value;
							break;
						case 3:
							$criteria_coeff 			= floatval($value);
							break;
						case 4:
							$criteria_type 				= $value;
							break;
						case 5:
							$criteria_mark_description 	= $value;
							break;
						case 6:
							$criteria_zero 				= $value;
							break;
					}
				}
				
				$criteria_id = $db->insertCriteria($criteria_name, $criteria_description, $criteria_coeff, $evaluation_id);
				
				if ($criteria_zero == 1)
				{
					$db->setToZero($criteria_id);
				}

				$i  = 0;
				
				foreach ($criteria_mark_description as $keya => $valuea) 
				{
					foreach ($valuea as $keyb => $valueb)
					{
						switch ($keyb) 
						{
							case 0:
								$id_level_mark = $valueb;
								break;
							
							case 1:
								$description_mark = $valueb;
								break;
							case 2:
								$checked = $valueb;
								break;
						}
					}

					if ($checked == 'checked')
					{
						$checked = 1;
					}
					else
					{
						$checked = 0;
					}

					$db->insertCriteriaMark($criteria_id, $evaluation_id, $mark_level[$i]['name'], $mark_level[$i]['mark'], $description_mark, $checked);
					$i++;

					if ($i == count($mark_level))
						$i = 0;
				}
			}
		}
	}

}
?>